package com.itboye.mengwu.app.ui.mymeng;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.BasePtr;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.adapter.MyBoyAdapter;
import com.itboye.mengwu.app.ui.aizhiwu.ActivityQueryWodeDongTaiDetail;
import com.itboye.mengwu.data.model.LuntanTieZiBean;
import com.itboye.mengwu.data.model.MyTieZiCenterBean;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.data.presenter.MyTieZiCenterPresenter;
import com.itboye.mengwu.data.presenter.TieZiPresenter;
import com.itboye.mengwu.testpic.ui.PublishedActivity;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

public class MyBoyActivity extends BaseActivity implements Observer {
    ArrayList<TieZiBaoHanReply.GetAList> list;
    MyBoyAdapter adapter;
    ListView listview;
    MyTieZiCenterPresenter lunTanPresenter;
    ImageView back;
    TextView txt_title;
    TieZiBaoHanReply baoHanReply = new TieZiBaoHanReply();
    ArrayList<String> arrayList = new ArrayList<String>();
    private MyTieZiCenterBean bean;
    TieZiPresenter presenter;
    LuntanTieZiBean model = new LuntanTieZiBean();
    int position;
    protected String uid;
    int pageNo = 1;
    String type = "refresh";
    PtrFrameLayout ptr;

    @Override
    public int initView() {
        // TODO Auto-generated method stub
        return R.layout.mybabyfun;
    }

    @Override
    public void onMyCreate() {
        // adapter.notifyDataSetChanged();
        BasePtr.setPagedPtrStyle(ptr);
        ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                showProgressDialog("正在刷新数据，请稍后", true);
                type = "refresh";
                pageNo++;
                model.setFid("4");
                model.setPage_no(pageNo + "");
                lunTanPresenter.getInfo(model);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                showProgressDialog("正在刷新数据，请稍后", true);
                pageNo = 1;
                type = "clean";
                model.setFid("4");
                model.setPage_no(pageNo + "");
                lunTanPresenter.getInfo(model);
            }
        });
        showProgressDialog("加载数据中，请稍后", true);
        uid = SPUtils.get(this, null, SpPublicParams.UID, "") + "";
        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
        txt_title.setText("我的宝宝乐");
        lunTanPresenter = new MyTieZiCenterPresenter(this);
        presenter = new TieZiPresenter(this);
        model.setUid(SPUtils.get(this, null, SpPublicParams.UID, "") + "");
        model.setFid("4");
        lunTanPresenter.getInfo(model);
        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent intent = new Intent(MyBoyActivity.this,
                        ActivityQueryWodeDongTaiDetail.class);
                intent.putStringArrayListExtra("imgUrl", baoHanReply.getList()
                        .get(position - 1).getImg());
                intent.putExtra("message",
                        baoHanReply.getList().get(position - 1).getMessage()
                                + "");
                intent.putExtra("tid", baoHanReply.getList().get(position - 1)
                        .getTid());
                intent.putExtra("isLike",
                        baoHanReply.getList().get(position - 1).getIsLike());
                System.out.println("isLike"
                        + baoHanReply.getList().get(position - 1).getIsLike()
                        + "sf");
                startActivityForResult(intent, 101);
            }
        });
        View view = LayoutInflater.from(this).inflate(R.layout.item_day, null);
        RelativeLayout today_lin = (RelativeLayout) view
                .findViewById(R.id.today_lin);
        today_lin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MyBoyActivity.this,

                        PublishedActivity.class);
                intent.putExtra("type", "4");
                startActivityForResult(intent, 101);// (intent);
            }
        });
        listview.addHeaderView(view);
        iniAdap();
    }

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        // TODO Auto-generated method stub
        super.onActivityResult(arg0, arg1, arg2);
        if (arg0 == 101 && arg1 == 102) {
            type = "clean";
            model.setUid(SPUtils.get(this, null, SpPublicParams.UID, "") + "");
            pageNo = 1;
            model.setPage_no(pageNo + "");
            model.setFid("4");
            lunTanPresenter.getInfo(model);
        }
    }

    public void iniAdap() {
        list = new ArrayList<>();
        adapter = new MyBoyAdapter(list, this, clickListener);
        listview.setAdapter(adapter);
    }

    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub
        ptr.refreshComplete();
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == MyTieZiCenterPresenter.QueryMyTieZiSuccess) {
                baoHanReply = (TieZiBaoHanReply) entity.getData();
                if (type.equals("clean")) {
                    list.clear();
                }
                list.addAll(baoHanReply.getList());
                adapter.notifyDataSetChanged();
            } else if (entity.getEventType() == MyTieZiCenterPresenter.QueryMyTieZiFail) {

            } else if (entity.getEventType() == TieZiPresenter.TieziDeleteSuccess) {
                // ByAlert.alert(entity.getData() + "");
                model.setFid("4");
                pageNo = 1;
                model.setPage_no(pageNo + "");
                lunTanPresenter.getInfo(model);
            } else if (entity.getEventType() == TieZiPresenter.TieziDeleteFail) {
                // ByAlert.alert(entity.getData() + "");
                adapter.notifyDataSetChanged();
            }
            try {
                closeProgressDialog();
            } catch (Exception e) {
            }
            if (pageNo <= 10) {

            } else {

            }
        }
    }

    OnClickListener clickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            showProgressDialog("正在删除中,请稍后", true);
            position = Integer.parseInt(v.getTag() + "");
            LuntanTieZiBean bean = new LuntanTieZiBean();
            type = "clean";
            bean.setUid(uid);
            bean.setFid("4");
            bean.setTid(list.get(position).getTid());
            presenter.delete(bean);
        }
    };
}
